/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *         http://www.apexes.net
 *
 */
package net.apexes.codegen.core;

import com.querydsl.codegen.EntityType;
import com.querydsl.codegen.Property;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 
 * @author <a href="mailto:hedyn@foxmail.com">HeDYn</a>
 *
 */
public final class CodegenUtils {
    
    private CodegenUtils() {}

    /**
     * 获取父类中没有的属性
     */
    public static List<Property> getProperties(EntityType model, Comparator<Property> columnComparator) {
        com.querydsl.codegen.Supertype supertype = model.getSuperType();
        if (supertype != null) {
            Class<?> superClass = supertype.getType().getJavaClass();
            return getProperties(model, superClass, columnComparator);
        }
        return getProperties(model, null, columnComparator);
    }
    
    /**
     * 获取父类中没有的属性
     */
    public static List<Property> getProperties(EntityType model, Class<?> superClass, Comparator<Property> columnComparator) {
        List<Property> properties;
        if (superClass != null) {
            properties = new ArrayList<>();
            for (Property property : model.getProperties()) {
                String propertyName = property.getEscapedName();
                if (findField(superClass, propertyName) == null) {
                    properties.add(property);
                }
            }
        } else {
            properties = new ArrayList<>(model.getProperties());
        }
        if (columnComparator != null) {
            properties.sort(columnComparator);
        }
        return properties;
    }
    
    public static Field findAccessibleField(Class<?> classType, String fieldName) {
        Field field = findField(classType, fieldName);
        if (field != null) {
            int modifiers = field.getModifiers();
            if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers)) {
                return field;
            }
        }
        return null;
    }
    
    private static Field findField(Class<?> classType, String fieldName) {
        if (classType == null || classType == Object.class) {
            return null;
        }
        try {
            return classType.getDeclaredField(fieldName);
        } catch (Exception ignore) {
        }
        return findField(classType.getSuperclass(), fieldName);
    }
    
    /**
     * 获取指定类的名称
     */
    public static String getTypeName(Class<?> classType) {
        if (classType.isArray()) {
            try {
                Class<?> cl = classType;
                int dimensions = 0;
                while (cl.isArray()) {
                    dimensions++;
                    cl = cl.getComponentType();
                }
                StringBuilder sb = new StringBuilder();
                sb.append(cl.getName());
                for (int i = 0; i < dimensions; i++) {
                    sb.append("[]");
                }
                return sb.toString();
            } catch (Throwable e) { /*FALLTHRU*/ }
        }
        return classType.getName();
    }

    public static boolean isSuperMethod(Class<?> superclass, String methodName) {
        while (superclass != Object.class) {
            for (Method method : superclass.getDeclaredMethods()) {
                if (methodName.equals(method.getName())) {
                    int m = method.getModifiers();
                    if (Modifier.isPublic(m) || Modifier.isProtected(m)) {
                        return true;
                    }
                }
            }
            superclass = superclass.getSuperclass();
        }
        return false;
    }

}
